package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.IOfflineEventsDao;
import com.magicode.cms.entiy.base.Article;
import com.magicode.cms.entiy.base.News;
import com.magicode.cms.entiy.base.NewsEnterpriseRela;
import com.magicode.cms.entiy.base.OfflineEvents;
import com.magicode.cms.entiy.base.OfflineEventsEnterprise;
import com.magicode.cms.entiy.base.OfflineEventsUser;
import com.magicode.cms.entiy.base.Picture;
import com.magicode.cms.entiy.base.ProgramPersonRela;
import com.magicode.cms.entiy.base.User;
import com.magicode.cms.service.IOfflineEventsService;
@Service("serviceOfflineEvents")
public class OfflineEventsServiceImpl extends BaseServiceImpl<OfflineEvents,IOfflineEventsDao> implements IOfflineEventsService,UtilPath {

	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql ="from " +OfflineEvents.class.getName()+" "+entity_alias_name+" "
				+ " left join t.city as ct "
				+ " left join t.province as prv "
				+ " left join t.eventSubject evt  "
				+ "where 1 = 1  ";
		if(null!=map && !map.isEmpty()){
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+map.get(field_id)+"'";
			}
			if(map.containsKey("provinceId")){
				hql = hql+" and "+entity_alias_name+".province.id = '"+map.get("provinceId")+"'";
			}
			if(map.containsKey("cityId")){
				hql = hql+" and "+entity_alias_name+".city.id = '"+map.get("cityId")+"'";
			}
			if(map.containsKey("typeId")){
				hql = hql+" and "+entity_alias_name+".eventType.id = '"+map.get("typeId")+"'";
			}
			if(map.containsKey("subjectId")){
				hql = hql+" and "+entity_alias_name+".eventSubject.id = '"+map.get("subjectId")+"'";
			}
			if(map.containsKey("title")){
				hql = hql+" and "+entity_alias_name+".title like '%"+map.get("title")+"%'";
			}
//			if(map.containsKey("title")){
//				hql = hql+" and "+entity_alias_name+".title like '%"+map.get(field_id)+"%'";
//			}
			if(map.containsKey("beginDateFrom")){
				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".beginTime,'"+sql_date_format+"') >= '"+map.get("beginDateFrom")+"'";
			}
			if(map.containsKey("beginDateTo")){
				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".beginTime,'"+sql_date_format+"') <= '"+map.get("beginDateTo")+"'";
			}
			if(map.containsKey("endDateFrom")){
				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".endTime,'"+sql_date_format+"') >= '"+map.get("endDateFrom")+"'";
			}
			if(map.containsKey("endDateTo")){
				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".endTime,'"+sql_date_format+"') <= '"+map.get("endDateTo")+"'";
			}
			if(map.containsKey("status")){
				hql = hql+" and "+entity_alias_name+".status = '"+map.get("status")+"'";
			}
		}
		return hql;
	}
	@Override
	public List<Map<String, Object>> getBeanInfo() {

		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "标题");
		map.put("width", 200);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "beginTime");
		map.put(json_title, "开始时间");
		map.put("width", 90);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "eventSubject");
		map.put(json_title, "主题");
		map.put("width", 50);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "city");
		map.put(json_title, "城市");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "attendUserCount");
		map.put(json_title, "参加人数");
		map.put("width", 50);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "resumeCoin");
		map.put(json_title, "豆币");
		map.put("width", 50);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "shareCommentsCount");
		map.put(json_title, "查看|评论");
		map.put("width", 70);
		beaninfo.add(map);
		
		return beaninfo;
	}

	@Override
	public List<JsonOperation> getOpts(String url,String id) {
		List<JsonOperation> opts = new ArrayList<JsonOperation> ();
		JsonOperation opt= new JsonOperation();
		opt.setName("修改");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/getinfo/"+id+"/edit"+url);
		opt.setType("edit");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("删除");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/"+id+"?_requestMethod=delete");
		opt.setType("delete2");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("预览");
		opt.setUrl(getCtx().getContextPath()+"/p/offline/sec/"+id+"");
		opt.setType("preview");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("参加用户");
		opt.setUrl(getCtx().getContextPath()+"/offlineeventsuser");
		opt.setType("offlineuser");
		opts.add(opt);
		return opts;
	}
	@Override
	public IOfflineEventsDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return offlineEventsDao;
	}
	@Autowired
	@Qualifier("daoOfflineEvents")
	private IOfflineEventsDao offlineEventsDao;
	
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("beginTime", ""+sql_date_method+"("+entity_alias_name+".beginTime,'"+sql_date_format+"') as beginTime ");
			gridsel.put("endTime", ""+sql_date_method+"("+entity_alias_name+".endTime,'"+sql_date_format+"')  as endTime ");
			gridsel.put("eventSubject", "evt.title as eventSubject ");
			gridsel.put("city", "prv.title || ' ' || "
					+ "ct.title as city ");
			gridsel.put("attendUserCount", ""+entity_alias_name+".attendUserCount as attendUserCount ");
			gridsel.put("resumeCoin", ""+entity_alias_name+".resumeCoin as resumeCoin ");
			gridsel.put("shareCommentsCount", ""+entity_alias_name+".clickCount || '|' || "+entity_alias_name+".commentsCount "
					+ "  as shareCommentsCount ");
			return this.queryMapDataList(hql, gridsel, start, size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	}
	
	@Override
	public List<OfflineEventsEnterprise> getOfflineEventsEnterprise(String id) {
		List<OfflineEventsEnterprise> oeList = new ArrayList();
		String sql = " select oe from "+OfflineEventsEnterprise.class.getName()+" oe left outer join fetch oe.enterprise e left join oe.offlineEvents o "
				+ " where o.id = :id order by oe.id ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			oeList = this.offlineEventsDao.getCommonDaoService().queryList(sql, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return oeList;
	}
	
	/*
	 *上传多个配图
	 */
	@Override
	public List<Picture> getPictures(String id) {
		List<Picture> picList = new ArrayList();
		String sql = " select p from "+Picture.class.getName()+" p , "+News.class.getName()+" t "
				+ " where p in elements(t.relaPics) and t.id = :id order by p.id ";
		Article tvFilmPerson = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			picList = this.offlineEventsDao.getCommonDaoService().queryList(sql, map);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		return picList;
	}
	
	@Override
	public void deletPiclist(String userid){
		
		this.offlineEventsDao.deletPiclist(userid);
	}
	
	public IOfflineEventsDao getOfflineEventsDao() {
		return offlineEventsDao;
	}
	public void setOfflineEventsDao(IOfflineEventsDao offlineEventsDao) {
		this.offlineEventsDao = offlineEventsDao;
	}
	@Override
	public List<OfflineEventsEnterprise> getOfflineEventsCoSponsorEnterprise(
			String id) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
